from typing import Optional, Any, Dict, List
from pydantic import BaseModel, Field
from datetime import datetime

from app.schemas.base import BaseSchema, TimestampMixin, BaseEntity, PageQuery


# 租户查询参数
class TenantQuery(PageQuery):
    """租户查询参数"""
    tenantId: Optional[str] = None
    contactUserName: Optional[str] = None
    companyName: Optional[str] = None
    status: Optional[str] = None


# 租户表单和响应
class TenantForm(BaseModel):
    """租户表单 - 匹配RuoYi sys_tenant表结构"""
    id: Optional[int] = None
    tenantId: str = Field(..., max_length=20, description="租户编号")
    contactUserName: Optional[str] = Field(None, max_length=20, description="联系人")
    contactPhone: Optional[str] = Field(None, max_length=20, description="联系电话")
    companyName: Optional[str] = Field(None, max_length=50, description="企业名称")
    licenseNumber: Optional[str] = Field(None, max_length=30, description="统一社会信用代码")
    address: Optional[str] = Field(None, max_length=200, description="地址")
    intro: Optional[str] = Field(None, max_length=200, description="企业简介")
    domain: Optional[str] = Field(None, max_length=200, description="域名")
    packageId: Optional[int] = Field(None, description="租户套餐编号")
    expireTime: Optional[str] = Field(None, description="过期时间")
    accountCount: Optional[int] = Field(-1, description="用户数量（-1不限制）")
    status: str = Field('0', pattern=r"^[01]$", description="租户状态（0正常 1停用）")
    remark: Optional[str] = Field(None, max_length=200, description="备注")


class TenantVO(BaseEntity):
    """租户视图对象 - 匹配RuoYi sys_tenant表结构"""
    id: int
    tenantId: str
    contactUserName: Optional[str] = None
    contactPhone: Optional[str] = None
    companyName: Optional[str] = None
    licenseNumber: Optional[str] = None
    address: Optional[str] = None
    intro: Optional[str] = None
    domain: Optional[str] = None
    packageId: Optional[int] = None
    expireTime: Optional[str] = None
    accountCount: int
    status: str
    delFlag: str
    remark: Optional[str] = None


# 租户套餐相关
class TenantPackageQuery(PageQuery):
    """租户套餐查询参数"""
    packageName: Optional[str] = None
    status: Optional[str] = None


class TenantPackageForm(BaseModel):
    """租户套餐表单 - 匹配RuoYi sys_tenant_package表结构"""
    packageId: Optional[int] = None
    packageName: Optional[str] = Field(None, max_length=20, description="套餐名称")
    menuIds: Optional[str] = Field(None, max_length=3000, description="关联菜单id")
    menuCheckStrictly: int = Field(1, description="菜单树选择项是否关联显示")
    status: str = Field('0', pattern=r"^[01]$", description="状态（0正常 1停用）")
    remark: Optional[str] = Field(None, max_length=200, description="备注")


class TenantPackageVO(BaseEntity):
    """租户套餐视图对象 - 匹配RuoYi sys_tenant_package表结构"""
    packageId: int
    packageName: Optional[str] = None
    menuIds: Optional[str] = None
    menuCheckStrictly: int
    status: str
    delFlag: str
    remark: Optional[str] = None


# 兼容性保留
class TenantResponse(TenantVO):
    """租户响应 - 兼容性别名"""
    pass


class TenantListResponse(BaseModel):
    """租户列表响应"""
    total: int
    items: List[TenantVO]


class UpdateTenantStatusRequest(BaseModel):
    """更新租户状态请求"""
    tenantId: str = Field(..., description="租户ID")
    status: str = Field(..., pattern=r"^[01]$", description="状态（0正常 1停用）")


# 租户选项
class TenantOption(BaseModel):
    """租户选项"""
    tenantId: str
    companyName: str
    domain: Optional[str] = None